package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.CCemojiMsgUtil;
import com.bokecc.sdk.mobile.live.pojo.ChatMessage;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/3/29.
 */

public class LiveChatAdapter extends RecyclerView.Adapter<LiveChatAdapter.MyViewHolder> {

    //播主身份
    public final static String PUBLISHER = "publisher";

    private Context context;

    private List<ChatMessage> mList;

    private LayoutInflater layoutInflater;

    private String myId;

    private int PUBLISHER_COLOR;
    private int MY_COLOR;
    private int OTHER_COLOR;

    public LiveChatAdapter(Context context, String myId) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
        this.myId = myId;
        PUBLISHER_COLOR = context.getResources().getColor(R.color.live_chat_publisher_text_color);
        MY_COLOR = context.getResources().getColor(R.color.live_chat_my_text_color);
        OTHER_COLOR = context.getResources().getColor(R.color.live_chat_other_text_color);
    }

    public void setData(List<ChatMessage> mList) {
        if (this.mList != null) {
            this.mList.clear();
            this.mList.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(ChatMessage chatMessage) {
        if (this.mList != null) {
            this.mList.add(chatMessage);
            notifyDataSetChanged();
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LeftViewHolder(layoutInflater.inflate(R.layout.item_live_chat_left, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (mList != null && mList.size() > 0)
            holder.setData(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }


    public abstract class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView name, msg;

        public MyViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            msg = (TextView) itemView.findViewById(R.id.msg);
        }

        public abstract void setData(ChatMessage data);
    }

    public class LeftViewHolder extends MyViewHolder {

        public LeftViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(ChatMessage data) {
            name.setText(String.valueOf(data.getUserName() + " :"));
            msg.setText(CCemojiMsgUtil.parseFaceMsg(context,data.getMessage()));
            int textColor;
            if (TextUtils.equals(data.getUserRole(), PUBLISHER))
                textColor = PUBLISHER_COLOR;
            else if (TextUtils.equals(data.getUserId(), myId))
                textColor = MY_COLOR;
            else
                textColor = OTHER_COLOR;
            name.setTextColor(textColor);
        }
    }

   /* public class RightViewHolder extends MyViewHolder {

        public RightViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(ChatMessage data) {
            name.setText(String.valueOf(data.getUserName() + " :"));
            msg.setText(data.getMessage());
        }
    }*/

    public void clear() {
        if (mList != null) {
            mList.clear();
            mList = null;
        }
    }
}
